package org.infodavid.common.services;

import org.infodavid.common.impl.services.DefaultErrorHandler;
import org.infodavid.common.services.exceptions.IErrorHandler;
import org.infodavid.common.services.exceptions.ServiceException;

/**
 * The Class AbstractService.
 */
public class AbstractService<E extends Exception> {

	/** The error handler. */
	private IErrorHandler<? extends ServiceException> errorHandler = new DefaultErrorHandler();

	/**
	 * Instantiates a new helper.
	 */
	protected AbstractService() {
		super();
	}

	/**
	 * Handle .
	 * @param error the error
	 * @param source the source
	 * @param message the message
	 * @throws ServiceException the exception
	 */
	protected void handle(final Throwable error, final String message, final Object source)
	    throws ServiceException {
		if (error instanceof ServiceException) {
			throw (ServiceException)error;
		}

		if (errorHandler == null) {
			throw new ServiceException(error.getMessage());
		}

		errorHandler.handle(error, message, source);
	}

	/**
	 * Gets the error handler.
	 * @return the errorHandler
	 */
	public IErrorHandler<? extends ServiceException> getErrorHandler() {
		return errorHandler;
	}

	/**
	 * Sets the error handler.
	 * @param value the error handler
	 */
	public void setErrorHandler(final IErrorHandler<? extends ServiceException> value) {
		errorHandler = value;
	}
}
